package com.wang.controller;

import com.wang.luntan.common.pojo.ApiResponse;
import com.wang.luntan.common.util.PageInfoUtil;
import com.wang.luntan.common.web.controller.BaseController;
import com.wang.luntan.users.pojo.AtOpinionDesc;
import com.wang.luntan.users.pojo.AtOpinionType;
import com.wang.luntan.users.pojo.AtOpinionTypeEnum;
import com.wang.luntan.users.service.IOpinionDbService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户首页操作
 *
 * @author wangsh
 */
@Log4j2
@Controller
@RequestMapping("/back/opinion/")
public class OpinionBackController extends BaseController {
    @Autowired
    private IOpinionDbService opinionDbService;
    /**
     * 意见类型列表页面
     *
     * @return
     */
    @RequestMapping("/opinionTypeList")
    public String opinionTypeList(HttpServletRequest request, Model model) {
        log.info("--opinionTypeList--");
        /* 分页信息 */
        PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
        /* 搜索条件,生成公共的搜索条件 */
        Map<String, Object> condMap = this.proccedSearch(request);
        /* 查询数据库
         * 所有的数据以JSON的形式返回
         *  */
        ApiResponse<AtOpinionType> response = this.opinionDbService.findCondListOpinionTypeService(pageInfoUtil, condMap);
        /* 将结果存储到Request中 */
        model.addAttribute("response", response.toJSON());
        return "/back/opinion/opinionTypeList";
    }

    /**
     * 打开添加意见类型页面
     *
     * @return
     */
    @RequestMapping("/opinionTypeInsert")
    public String opinionTypeInsert() {
        log.info("--opinionTypeInsert--");
        return "/back/opinion/opinionTypeInsert";
    }

    /**
     * 添加意见类型提交操作
     *
     * @return
     */
    @RequestMapping(value = "/opinionTypeInsertSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String opinionTypeInsertSubmit(String pubTimeStr, AtOpinionType demoDynasty) {
        log.info("--opinionTypeInsertSubmit--");
        /* 为对象赋值 */
        demoDynasty.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
        demoDynasty.setCreateTime(new Date());
        demoDynasty.setUpdateTime(new Date());
        /* 保存结果 */
        ApiResponse response = this.opinionDbService.saveOneOpinionTypeService(demoDynasty);
        log.info("--意见类型添加返回结果:{}--", response.toJSON());
        return response.toJSON().toJSONString();
    }

    /**
     * 打开更新意见类型页面
     *
     * @return
     */
    @RequestMapping("/opinionTypeUpdate")
    public String opinionTypeUpdate(String id, String operType, Model model) {
        log.info("--opinionTypeUpdate--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<AtOpinionType> response = this.opinionDbService.findOneOpinionTypeService(condMap);
        /* 存储request */
        model.addAttribute("response", response.toJSON());

        if ("update".equalsIgnoreCase(operType)) {
            return "/back/opinion/opinionTypeUpdate";
        }
        return "/back/opinion/opinionTypeInfo";
    }

    /**
     * 添加意见类型提交操作
     *
     * @return
     */
    @RequestMapping(value = "/opinionTypeUpdateSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String opinionTypeUpdateSubmit(String id,
                                      HttpServletRequest request, Model model) {
        log.info("--opinionTypeUpdateSubmit--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<AtOpinionType> response = this.opinionDbService.findOneOpinionTypeService(condMap);
        /* 获取java对象 */
        AtOpinionType demoDynasty = response.getDataOneJava();

        /* 接收参数 */
        String name = request.getParameter("name");
        String content = request.getParameter("content");
        String status = request.getParameter("status");
        String pubTimeStr = request.getParameter("pubTimeStr");
        /* 设置属性 */
        demoDynasty.setName(name);
        demoDynasty.setContent(content);
        demoDynasty.setStatus(Byte.valueOf(status));

        demoDynasty.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
        demoDynasty.setUpdateTime(new Date());
        /* 数据库操作 */
        ApiResponse dbRes = opinionDbService.updateOneOpinionTypeService(demoDynasty);
        log.info("--意见类型添加返回结果:{}--", dbRes.toJSON());
        return dbRes.toJSON().toJSONString();
    }

    /**
     * 意见内容列表页面
     *
     * @return
     */
    @RequestMapping("/opinionDescList")
    public String opinionDescList(HttpServletRequest request, Model model) {
        log.info("--opinionDescList--");
        /* 分页信息 */
        PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
        /* 搜索条件,生成公共的搜索条件 */
        Map<String, Object> condMap = this.proccedSearch(request);
        String typeId = request.getParameter("typeId");
        condMap.put("typeId", typeId);
        request.setAttribute("typeId", typeId);

        String opType = request.getParameter("opType");
        condMap.put("opType", opType);
        request.setAttribute("opType", opType);

        String resolveStatus = request.getParameter("resolveStatus");
        condMap.put("resolveStatus", resolveStatus);
        request.setAttribute("resolveStatus", resolveStatus);

        String parentId = request.getParameter("parentId");
        if (StringUtils.isEmpty(parentId)) {
            /* 如果未传parentId,默认查询的是第一级 */
            parentId = "0";
        }
        condMap.put("parentId", parentId);
        request.setAttribute("parentId", parentId);

        /* 查询数据库
         * 所有的数据以JSON的形式返回
         *  */
        ApiResponse<AtOpinionDesc> response = this.opinionDbService.findCondListOpinionDescService(pageInfoUtil, condMap);
        /* 将结果存储到Request中 */
        model.addAttribute("response", response.toJSON());
        /* 查询所有的意见类型 */
        Map<String,Object> condParamsMap = new HashMap<>();
        condParamsMap.put("status", AtOpinionTypeEnum.STATUS_ENABLE.getStatus());
        ApiResponse<AtOpinionType> opinionTypeApiResponse = this.opinionDbService.findCondListOpinionTypeService(null, condParamsMap);
        model.addAttribute("opinionResponse", opinionTypeApiResponse.toJSON());
        return "/back/opinion/opinionDescList";
    }

    /**
     * 打开添加意见类型页面
     *
     * @return
     */
    @RequestMapping("/opinionDescInsert")
    public String opinionDescInsert(HttpServletRequest request, Model model) {
        log.info("--opinionDesceInsert--");
        String parentId = request.getParameter("parentId");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", parentId);
        /* 查询数据库 */
        ApiResponse<AtOpinionDesc> opinionDescParentResponse = this.opinionDbService.findOneOpinionDescService(condMap);
        model.addAttribute("opinionDescParentResponse", opinionDescParentResponse.toJSON());
        return "/back/opinion/opinionDescInsert";
    }

    /**
     * 添加意见类型提交操作
     *
     * @return
     */
    @RequestMapping(value = "/opinionDescInsertSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String opinionDescInsertSubmit(String pubTimeStr, AtOpinionDesc paramObj) {
        log.info("--opinionDescInsertSubmit--");
        /* 为对象赋值 */
        paramObj.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
        paramObj.setCreateTime(new Date());
        paramObj.setUpdateTime(new Date());
        /* 保存结果 */
        ApiResponse response = this.opinionDbService.saveOneOpinionDescService(paramObj);
        log.info("--意见类型添加返回结果:{}--", response.toJSON());
        return response.toJSON().toJSONString();
    }

    /**
     * 打开更新意见内容页面
     *
     * @return
     */
    @RequestMapping("/opinionDescUpdate")
    public String opinionDescUpdate(String id, String operType, Model model) {
        log.info("--opinionDescUpdate--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<AtOpinionDesc> response = this.opinionDbService.findOneOpinionDescService(condMap);
        /* 存储request */
        model.addAttribute("response", response.toJSON());

        if ("update".equalsIgnoreCase(operType)) {
            return "/back/opinion/opinionDescUpdate";
        }
        return "/back/opinion/opinionDescInfo";
    }

    /**
     * 添加意见内容提交操作
     *
     * @return
     */
    @RequestMapping(value = "/opinionDescUpdateSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String opinionDescUpdateSubmit(String id,
                                          HttpServletRequest request, Model model) {
        log.info("--opinionDescUpdateSubmit--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<AtOpinionDesc> response = this.opinionDbService.findOneOpinionDescService(condMap);
        /* 获取java对象 */
        AtOpinionDesc dataObj = response.getDataOneJava();

        /* 接收参数 */
        String content = request.getParameter("content");
        String status = request.getParameter("status");
        String resolveStatus = request.getParameter("resolveStatus");
        String pubTimeStr = request.getParameter("pubTimeStr");
        /* 设置属性 */
        dataObj.setResolveTime(new Date());
        dataObj.setResolveStatus(Byte.parseByte(resolveStatus));
        dataObj.setContent(content);
        dataObj.setStatus(Byte.parseByte(status));

        dataObj.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
        dataObj.setUpdateTime(new Date());
        /* 数据库操作 */
        ApiResponse dbRes = opinionDbService.updateOneOpinionDescService(dataObj);
        log.info("--意见内容添加返回结果:{}--", dbRes.toJSON());
        return dbRes.toJSON().toJSONString();
    }
}
